package com.cj.utils;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 自定义枚举类型处理器
 */

@MappedTypes(com.cj.pojo.UserRole.class)
public class CustomEnumTypeHandler extends BaseTypeHandler<com.cj.pojo.UserRole> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, com.cj.pojo.UserRole parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.getValue());
    }

    @Override
    public com.cj.pojo.UserRole getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String value = rs.getString(columnName);
        return value == null ? null : com.cj.pojo.UserRole.fromValue(value);
    }

    @Override
    public com.cj.pojo.UserRole getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String value = rs.getString(columnIndex);
        return value == null ? null : com.cj.pojo.UserRole.fromValue(value);
    }

    @Override
    public com.cj.pojo.UserRole getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String value = cs.getString(columnIndex);
        return value == null ? null : com.cj.pojo.UserRole.fromValue(value);
    }
}
